<template>
  <smart-table
    :title="postTitle"
    :formList="postFormList"
    :selectUrl="postSelectUrl"
    :insertUrl="postInsertUrl"
    :updateUrl="postUpdateUrl"
    :deleteUrl="postDeleteUrl"
    :exportExcelUrl="postExportUrl"
    :importExcelUrl="postImportUrl"
    :tableColumns="postTableColumns"
    :formButtonCtl="postFormButtonCtl"
    :formRules="postFormRules"
    :openDialogMethodExtend="coverTypeList"
    :dialogWidth="60"
    :insertUpdateMethodExtend="addHRUserId"
    :dialogLabelWidth="12"
    :maxOptionButtonCount="6">
    <template slot="table-button" slot-scope="row">
      <el-button type="warning" icon="el-icon-tickets" @click="toSubmit(row.row.id)">投递记录</el-button>
    </template>
    <template slot="table-custom-column" slot-scope="row">
      {{ showAddressStr(row.item.address) }}
    </template>
  </smart-table>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  computed: {
    ...mapGetters(['id']),
  },
  data() {
    return {
      //绑定弹窗
      bindDialogVisible: false,
      //当前岗位
      currentPost: {},
      //分类树
      postTree: [],
      //树过滤
      treeFilterText: '',
      //主题
      postTitle: '岗位',
      //表单列表
      postFormList: [
        {
          title: '名称',
          name: 'name',
          type: 'input',
          width: 80,
        },
        {
          title: '岗位职责',
          name: 'duty',
          type: 'textarea',
          width: 80,
          hidden: {
            header: true,
          },
        },
        {
          title: '岗位要求',
          name: 'requirement',
          type: 'textarea',
          width: 80,
          hidden: {
            header: true,
          },
        },
        {
          title: '岗位分类',
          name: 'postCategoryId',
          type: 'tree',
          optionList: [],
          value: 'id',
          label: 'name',
          children: 'children',
          emitPath: false,
          expandTrigger: 'hover',
          width: 80,
        },
        {
          title: '工作地点',
          name: 'address',
          type: 'tree',
          width: 80,
          hidden: {
            header: true,
          },
          optionList: [],
          value: 'name',
          label: 'name',
          children: 'districts',
        },
        {
          title: '最少时间',
          name: 'minWork',
          type: 'number',
          numberRange: {
            min: 0,
            max: 10,
          },
          hidden: {
            header: true,
          },
        },
        {
          title: '最长时间',
          name: 'maxWork',
          type: 'number',
          hidden: {
            header: true,
          },
          numberRange: {
            min: 0,
            max: 10,
          },
        },
        {
          title: '最低工资',
          name: 'minSalary',
          type: 'number',
          numberRange: {
            min: 2000,
          },
          hidden: {
            header: true,
          },
        },
        {
          title: '最高工资',
          name: 'maxSalary',
          type: 'number',
          hidden: {
            header: true,
          },
          numberRange: {
            min: 2000,
          },
        },
        {
          title: '面向群体',
          name: 'group',
          type: 'select',
          width: 80,
          optionList: [],
          showValue: false,
        },
        {
          title: '学历要求',
          name: 'education',
          type: 'select',
          optionList: [],
          showValue: false,
          width: 80,
        },
        {
          title: '招聘类型',
          name: 'type',
          type: 'select',
          optionList: [],
          showValue: false,
          width: 80,
        },
        {
          title: '招聘状态',
          name: 'status',
          type: 'select',
          optionList: [],
          showValue: false,
          width: 80,
        },
      ],
      //岗位查询路径
      postSelectUrl: 'post/lookPost',
      //岗位新增路径
      postInsertUrl: 'post/insertOnePost',
      //岗位更新路径
      postUpdateUrl: 'post/updateOnePost',
      //岗位删除路径
      postDeleteUrl: 'post/deletePost',
      //岗位导出路径
      postExportUrl: '/recruitService/post/export',
      //岗位导入路径
      postImportUrl: '/recruitService/post/import',
      //表格列
      postTableColumns: [
        {
          title: '名称',
          name: 'name',
        },
        {
          title: '岗位分类',
          name: 'postCategoryName',
        },
        {
          title: '工作地点',
          name: 'address',
          type: 'custom_address',
        },
        {
          title: '最少时间',
          name: 'minWork',
          suffix: '年',
        },
        {
          title: '最长时间',
          name: 'maxWork',
          suffix: '年',
        },
        {
          title: '最低工资',
          name: 'minSalary',
          suffix: '元/月',
        },
        {
          title: '最高工资',
          name: 'maxSalary',
          suffix: '元/月',
        },
        {
          title: '面向群体',
          name: 'group',
          type: 'tag',
          tagStyle: {},
          filters: [],
          tipMap: {},
          width: 120,
        },
        {
          title: '学历要求',
          name: 'education',
          type: 'tag',
          tagStyle: {},
          filters: [],
          tipMap: {},
        },
        {
          title: '招聘类型',
          name: 'type',
          type: 'tag',
          tagStyle: {},
          filters: [],
          tipMap: {},
        },
        {
          title: '招聘状态',
          name: 'status',
          type: 'tag',
          tagStyle: {},
          filters: [],
          tipMap: {},
        },
        {
          title: '创建时间',
          name: 'createTime',
          width: 180,
        },
        {
          title: '修改时间',
          name: 'updateTime',
          width: 180,
        },
      ],
      //表单按钮控制
      postFormButtonCtl: {
        detailButton: false,
      },
      //表单校验
      postFormRules: {
        name: [
          {
            required: true,
            message: '岗位名不得为空!',
            trigger: 'blur',
          },
        ],
        duty: [
          {
            required: true,
            message: '岗位职责不得为空!',
            trigger: 'blur',
          },
        ],
        requirement: [
          {
            required: true,
            message: '岗位要求不得为空!',
            trigger: 'blur',
          },
        ],
        postCategoryId: [
          {
            required: true,
            message: '岗位分类不得为空!',
            trigger: 'change',
          },
        ],
        education: [
          {
            required: true,
            message: '学历要求不得为空!',
            trigger: 'change',
          },
        ],
        address: [
          {
            required: true,
            message: '岗位地址不得为空!',
            trigger: 'change',
          },
        ],
        group: [
          {
            required: true,
            message: '面向群体不得为空!',
            trigger: 'change',
          },
        ],
        type: [
          {
            required: true,
            message: '招聘类型不得为空!',
            trigger: 'change',
          },
        ],
        status: [
          {
            required: true,
            message: '招聘状态不得为空!',
            trigger: 'change',
          },
        ],
      },
      //面向群体
      groupMap: {
        style: {
          0: 'warning',
          1: 'success',
        },
        tip: {
          0: '社会招聘',
          1: '校园招聘',
        },
        filters: [
          { title: '社会招聘', name: 0 },
          { title: '校园招聘', name: 1 },
        ],
      },
      //学历要求
      educationMap: {
        style: {
          0: 'success',
          1: 'info',
          2: 'warning',
          3: 'danger',
        },
        tip: {
          0: '不限',
          1: '大专及以上',
          2: '本科及以上',
          3: '研究生及以上',
        },
        filters: [
          { title: '不限', name: 0 },
          { title: '大专及以上', name: 1 },
          { title: '本科及以上', name: 2 },
          { title: '研究生及以上', name: 3 },
        ],
      },
      //类型
      typeMap: {
        style: {
          0: 'success',
          1: 'warning',
          2: 'danger',
        },
        tip: {
          0: '全职',
          1: '兼职',
          2: '实习',
        },
        filters: [
          { title: '全职', name: 0 },
          { title: '兼职', name: 1 },
          { title: '实习', name: 2 },
        ],
      },
      //状态
      statusMap: {
        style: {
          0: 'warning',
          1: 'success',
          2: 'danger',
        },
        tip: {
          0: '未开始',
          1: '进行中',
          2: '已结束',
        },
        filters: [
          { title: '未开始', name: 0 },
          { title: '进行中', name: 1 },
          { title: '已结束', name: 2 },
        ],
      },
    }
  },
  created() {
    this.getPostCategoryTree()
    this.getChinaDistrict()
    this.addConfig()
  },
  methods: {
    //合并属性
    addConfig() {
      // 表单
      this.$set(this.postFormList[9], 'optionList', this.groupMap.filters)
      this.$set(this.postFormList[10], 'optionList', this.educationMap.filters)
      this.$set(this.postFormList[11], 'optionList', this.typeMap.filters)
      this.$set(this.postFormList[12], 'optionList', this.statusMap.filters)
      // 表格
      this.$set(this.postTableColumns[7], 'filters', this.groupMap.filters)
      this.$set(this.postTableColumns[7], 'tagStyle', this.groupMap.style)
      this.$set(this.postTableColumns[7], 'tipMap', this.groupMap.tip)

      this.$set(this.postTableColumns[8], 'filters', this.educationMap.filters)
      this.$set(this.postTableColumns[8], 'tagStyle', this.educationMap.style)
      this.$set(this.postTableColumns[8], 'tipMap', this.educationMap.tip)

      this.$set(this.postTableColumns[9], 'filters', this.typeMap.filters)
      this.$set(this.postTableColumns[9], 'tagStyle', this.typeMap.style)
      this.$set(this.postTableColumns[9], 'tipMap', this.typeMap.tip)

      this.$set(this.postTableColumns[10], 'filters', this.statusMap.filters)
      this.$set(this.postTableColumns[10], 'tagStyle', this.statusMap.style)
      this.$set(this.postTableColumns[10], 'tipMap', this.statusMap.tip)
    },
    // 获取分类树状图
    getPostCategoryTree() {
      this.$store.dispatch('postCategory/lookPostCategoryTree').then((response) => {
        this.postFormList[3].optionList = response.data
      })
    },
    // 获取中国省市区
    getChinaDistrict() {
      this.$store.dispatch('map/getChinaDistrict').then((response) => {
        this.postFormList[4].optionList = response
      })
    },
    // 查看已投递的简历
    toSubmit(id) {
      this.$router.push({
        name: 'ResumeSubmit',
        query: {
          postId: id,
        },
      })
    },
    // 添加HR用户id
    addHRUserId(form) {
      form.userId = this.id
      return form
    },
    // 覆盖类型列表
    coverTypeList(type, form) {
      if (!form.address) {
        return form
      }
      form.address = JSON.parse(form.address)
      return form
    },
    // 显示省市区字符串
    showAddressStr(address) {
      var addressObj = JSON.parse(address)
      return addressObj.join('')
    },
  },
}
</script>

<style></style>
